package utils;

import java.util.Arrays;
import java.util.List;

/**
 * @Description:分页工具类
 * @Author one world
 * @Date 2020/9/9 0009 22:08
 */
public class PageInfo<T> {
    List<T> list;//存放对象
    private Integer total=0;//总数
    private Integer pageNum=10;//总页数
    private Integer limit=10;//每页条数
    private Integer currPage=1;//当前页
    private boolean isLast =false;//是否最后
    private boolean isFirst=false;
    private boolean hasNext=false;
    private boolean hasPrefix=false;
    private  Integer[] nav;//导航的数组
    private Integer navNum=10;//导航的数量

    /**
     * 构造方法初始化PageInfo
     * @param total
     * @param limit
     */
    public PageInfo(Integer total,Integer limit){
    init(1, total, limit);
    }

    /**
     * 构造方法初始化PageInfo
     * @param currPage
     * @param total
     * @param limit
     */
    public PageInfo(Integer currPage,Integer total,Integer limit){
        init(currPage, total, limit);
    }

    /**
     * 给类属性计算并赋值
     * @param currPage
     * @param total
     * @param limit
     */
    public void init(Integer currPage,Integer total,Integer limit){
        this.total=total;
        this.limit=limit;
        this.pageNum=total%limit==0?total/limit:(total/limit+1);
        if(currPage<1){
            this.currPage=1;
        }else if(currPage>pageNum){
            this.currPage=pageNum;
        }else{
            this.currPage=currPage;
        }
        initNav();
        check();
    }

    /**
     * 初始化导航栏 1 2 3...
     */
    public void initNav(){
       if(pageNum<=navNum){
           nav = new Integer[pageNum];
           for(int i=0;i<pageNum;i++){
              nav[i]=i+1;
           }
       }else{
           nav = new Integer[navNum];
           int start = currPage-navNum/2;
           int end = currPage+navNum/2;
           if(start<1){
               start=1;
               for(int i=0;i<navNum;i++){
                  nav[i]=start++;
               }
           }else if (end>pageNum){
               end=pageNum;
              for (int i=0;i<navNum;i++){
                  nav[i]=end--;
              }
           }else{
               //中间页
               for(int i=0;i<navNum;i++){
                   nav[i]=start++;
               }
           }
       }
    }

    /**
     * 查看当前页的状态  有上一页/有下一页...
     */


    public void check(){
        isLast=currPage==pageNum;
        isFirst=currPage==1;
        hasNext=currPage<pageNum;
        hasPrefix=currPage>1;
    }

    /**
     * 获取数据List
     * @return
     */
    public List<T> getList() {
        return list;
    }

    /**
     * 设置list
     * @param list
     */
    public void setList(List<T> list) {
        this.list = list;
    }

    /**
     * 获取总页
     * @return
     */
    public Integer getTotal() {
        return total;
    }

    /**
     * 设置总页数
     * @param total
     */
    public void setTotal(Integer total) {
        this.total = total;
    }

    /**
     * 获取总页数
     * @return
     */
    public Integer getPageNum() {
        return pageNum;
    }

    /**
     * 设置总页数
     * @param pageNum
     */
    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    /**
     * 获取分页的值
     * @return
     */
    public Integer getLimit() {
        return limit;
    }

    /**
     * 设置分页的值
     * @param limit
     */
    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    /**
     * 获取当前页
     * @return
     */
    public Integer getCurrPage() {
        return currPage;
    }

    /**
     * 设置当前页
     * @param currPage
     */
    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
    }

    /**
     * 判断是否为最后一页
     * @return
     */
    public boolean isLast() {
        return isLast;
    }

    /**
     * 设置最后一页
     * @param last
     */
    public void setLast(boolean last) {
        isLast = last;
    }

    /**
     * 是否为第一页
     * @return
     */
    public boolean isFirst() {
        return isFirst;
    }

    /**
     * 设置第一页
     * @param first
     */
    public void setFirst(boolean first) {
        isFirst = first;
    }

    /**
     * 是否有下页
     * @return
     */
    public boolean isHasNext() {
        return hasNext;
    }

    /**
     * 设置是否有下页
     * @param hasNext
     */
    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }

    /**
     * 是否有前一页
     * @return
     */
    public boolean isHasPrefix() {
        return hasPrefix;
    }

    /**
     * 设置是否有前一页
     * @param hasPrefix
     */
    public void setHasPrefix(boolean hasPrefix) {
        this.hasPrefix = hasPrefix;
    }

    /**
     * 获取底部导航
     * @return
     */
    public Integer[] getNav() {
        return nav;
    }

    /**
     * 设置底部导航
     * @param nav
     */
    public void setNav(Integer[] nav) {
        this.nav = nav;
    }

    /**
     * 获取导航数量
     * @return
     */
    public Integer getNavNum() {
        return navNum;
    }
 
    public void setNavNum(Integer navNum) {
        this.navNum = navNum;
    }

    @Override
    public String toString() {
        return "PageInfo{" +
                "list=" + list +
                ", total=" + total +
                ", pageNum=" + pageNum +
                ", limit=" + limit +
                ", currPage=" + currPage +
                ", isLast=" + isLast +
                ", isFirst=" + isFirst +
                ", hasNext=" + hasNext +
                ", hasPrefix=" + hasPrefix +
                ", nav=" + Arrays.toString(nav) +
                ", navNum=" + navNum +
                '}';
    }
}
